<template>
  <div class="login-container">
    <!-- 左侧图片区域 -->
    <div class="login-image">
      <div class="image-overlay"></div>
    </div>

    <!-- 右侧登录区域 -->
    <div class="login-form">
      <el-card class="login-card">
        <template #header>
          <div class="login-header">
            <h2>音乐后台管理系统</h2>
          </div>
        </template>

        <el-form @submit.prevent="handleLogin">
          <el-form-item>
            <el-input
                v-model="form.username"
                placeholder="请输入用户名"
                prefix-icon="User"
            />
          </el-form-item>

          <el-form-item>
            <el-input
                v-model="form.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
            />
          </el-form-item>

          <el-form-item>
            <el-button
                type="primary"
                native-type="submit"
                class="login-btn"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
// 导入userApi
import api from '@/services/api'

const router = useRouter()
const form = ref({
  username: '',
  password: ''
})

const handleLogin = async () => {
  try {
    // 调用登录接口
    const response = await api.userApi.login({
      usernameOrEmail: form.value.username,
      password: form.value.password
    });

    // 检查响应是否成功
    if (response.code === 200 && response.success) {
      // 存储token到localStorage
      localStorage.setItem('auth_token', response.data.token);
      // 存储用户信息到localStorage
      localStorage.setItem('user_info', JSON.stringify(response.data.userInfo));
      // 跳转到仪表盘页面
      router.push('/admin/dashboard');
    } else {
      // 显示错误消息
      alert(response.message || '登录失败');
    }
  } catch (error) {
    // 处理网络错误或其他异常
    console.error('登录出错:', error);
    alert('发生错误，请重试');
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  position: relative;
}

/* 图片区域 */
.login-image {
  flex: 1;
  background-image: url('../assets/background.jpg'); /* 替换为您的图片路径 */
  background-size: cover;
  background-position: center;
  position: relative;
  min-width: 60%;
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /*background: linear-gradient(to right, rgba(0, 0, 0, 0.7), transparent);*/
}

/* 右侧登录区域样式 */
.login-form {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  min-width: 40%;
}

.login-card {
  width: 400px;
  border-radius: 8px;
  background-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.login-header {
  text-align: center;
  padding: 20px 0;
}

.login-header h2 {
  margin: 0;
  color: #333;
  font-weight: 600;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  margin-top: 10px;
}

/* 响应式设计 - 小屏幕时改为垂直布局 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .login-image {
    height: 40vh;
    min-width: 100%;
  }

  .login-form {
    min-width: 100%;
    padding: 20px;
  }

  .login-card {
    width: 100%;
    max-width: 400px;
  }
}
</style>